package khtn.mylocation;

import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class CategoriesActivity extends ListActivity  {
   
	private EfficientAdapter adap;
	private static String[] data = new String[] { "0", "1", "2", "3", "4", "5" };
	private static List<Location> locations = null;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    setContentView(R.layout.listcategories);
	    Location data = new Location(this.getApplication().getApplicationContext());
	    locations = data.getAll();
	    adap = new EfficientAdapter(this);
	    setListAdapter(adap);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		//Toast.makeText(this, "Click-" + String.valueOf(position), Toast.LENGTH_SHORT).show();
	}
	
	public static class EfficientAdapter extends BaseAdapter implements Filterable {
		private LayoutInflater mInflater;
		private Context context;
		 
		public EfficientAdapter(Context context) {
			//Cache the LayoutInflate to avoid asking for a new one each time.
			mInflater = LayoutInflater.from(context);
			this.context = context;
		}

		public View getView(final int position, View convertView, ViewGroup parent) {
			// A ViewHolder keeps references to children views to avoid unneccessary calls to findViewById() on each row.
			ViewHolder holder;
			Resources res = this.context.getResources();
			// When convertView is not null, we can reuse it directly, there is no need to reinflate it. We only inflate a new View when the convertView supplied by ListView is null.
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.categories, null);
		 
				// Creates a ViewHolder and store references to the two children views we want to bind data to.
				holder = new ViewHolder();
				
				holder.icon = (ImageView) convertView.findViewById(R.id.categoryIcon);
				holder.cate = (TextView) convertView.findViewById(R.id.categoryName);
				holder.number = (TextView) convertView.findViewById(R.id.countNumber);
				 
				convertView.setOnClickListener(new OnClickListener() {
					private int pos = position;
					 
					@Override
					public void onClick(View v) {
						Toast.makeText(context, "Click-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();   
					}
				});
					
				convertView.setTag(holder);
			} else {
				// Get the ViewHolder back to get fast access to the TextView and the ImageView.
				holder = (ViewHolder) convertView.getTag();
			}
			
			// Bind the data efficiently with the holder.
			holder.icon.setImageDrawable(GetIcon(position, res));
			holder.cate.setText(Helper.CategoryNames[position]);
			holder.number.setText("(" + getCountNumber(position) + ")");
			return convertView;
		}
		 
		static class ViewHolder {
			TextView cate;
			TextView number;
			ImageView icon;
		}
		
		private Drawable GetIcon(int position, Resources res) {
			switch (position) {
				case 0:
					return res.getDrawable(R.drawable.all);
				case 1:
					return res.getDrawable(R.drawable.uncategorized);
				case 2:
					return res.getDrawable(R.drawable.educ);
				case 3:
					return res.getDrawable(R.drawable.food);
				case 4:
					return res.getDrawable(R.drawable.games);
				case 5:
					return res.getDrawable(R.drawable.shopping);
				default:
					return res.getDrawable(R.drawable.ic_launcher);
			}			
		}
		
		private int getCountNumber(int position){
			int countNumber = 0, intLoop = 0;
			switch (position) {
				case 0:
					return locations.size();
				case 1:
					countNumber = 0;
					for(intLoop = 0; intLoop < locations.size(); ++intLoop){
						if(locations.get(intLoop).getCategory() == 1) {
							++countNumber;
						}
					}
					return countNumber;
				case 2:
					countNumber = 0;
					for(intLoop = 0; intLoop < locations.size(); ++intLoop){
						if(locations.get(intLoop).getCategory() == 2) {
							++countNumber;
						}
					}
					return countNumber;
				case 3:
					countNumber = 0;
					for(intLoop = 0; intLoop < locations.size(); ++intLoop){
						if(locations.get(intLoop).getCategory() == 3) {
							++countNumber;
						}
					}
					return countNumber;
				case 4:
					countNumber = 0;
					for(intLoop = 0; intLoop < locations.size(); ++intLoop){
						if(locations.get(intLoop).getCategory() == 4) {
							++countNumber;
						}
					}
					return countNumber;
				case 5:
					countNumber = 0;
					for(intLoop = 0; intLoop < locations.size(); ++intLoop){
						if(locations.get(intLoop).getCategory() == 5) {
							++countNumber;
						}
					}
					return countNumber;
				default:
					return 0;
			}	
		}

		@Override
		public int getCount() {
			return data.length;
		}

		@Override
		public Object getItem(int position) {
			return data[position];
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public Filter getFilter() {
			return null;
		}
	}
}